home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / tcisam.zip / ADR.C next >
Text File  |  1987-08-21  |  7KB  |  290 lines

  1. /*
  2.  * ADR.C - address book.  Illustrates use of index file functions.
  3.  *
  4.  *                      Copyright (c) 1987, Jim Mischel
  5.  * Modifications:
  6.  *
  7.  * 08/21/87 - jim - original coding
  8.  *
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <conio.h>
  13. #include <ctype.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <index.h>
  17.  
  18. typedef struct {
  19.   char first_name[31],
  20.        last_name[31],
  21.        business[31],
  22.        street[31],
  23.        city[26],
  24.        state[3],
  25.        zip[10],
  26.        phone1[11],
  27.        phone2[11],
  28.        name_key[21];
  29. } address_rec;
  30.  
  31. #define ed printf("\033[2J")    /* erase display */
  32. #define el printf("\033[K")     /* erase line */
  33.  
  34. void cup(int row, int col);
  35. void edit_it(void);
  36. void change_item(int item);
  37. void get_ret(void);
  38. void usage(void);
  39. void lookup(void);
  40. void list_forward(void);
  41. void list_backward(void);
  42.  
  43. char *adr_file;                 /* file descriptor for indexed file routines */
  44. address_rec addr;
  45.  
  46. int main(int argc, char *argv[])
  47. {
  48.   unsigned offset;
  49.   char c;
  50.  
  51.   if (!argc) {
  52.     usage();
  53.     return(1);
  54.   }
  55.  
  56.   offset = (char *) &addr.name_key - (char *) &addr;
  57.  
  58.   if ((adr_file = iopen(argv[1],sizeof(address_rec),STRING,offset,1,NULL)) == NULL) {
  59.     printf("\nCannot open file %s\n",argv[1]);
  60.     printf("Error status is %X\n",ierrno);
  61.     return(1);
  62.   }
  63.  
  64.   do {
  65.     ed;
  66.     cup(3,34);  printf("ADDRESS BOOK");
  67.     cup(24,1);  printf("(L)ookup, (F)orward list, (B)ackward list, (Q)uit");
  68.     cup(23,1);  printf("Enter function ");
  69.     switch (c = toupper(getche())) {
  70.       case 'L' :
  71.         lookup();
  72.         break;
  73.       case 'F' :
  74.         list_forward();
  75.         break;
  76.       case 'B' : list_backward();
  77.     }
  78.   } while (c != 'Q');
  79.  
  80.   ed;
  81.   iclose(adr_file);
  82.   return(0);
  83. }
  84.  
  85. void lookup(void)
  86. {
  87.   char quit = 0;
  88.   char tempkey[24];
  89.  
  90.   do {
  91.     ed;                         /* erase display */
  92.     cup(3,34);  printf("ADDRESS BOOK");
  93.     cup(5,1);   printf(" 1. Key        :");
  94.     cup(6,1);   printf(" 2. First name :");
  95.     cup(7,1);   printf(" 3. Last name  :");
  96.     cup(8,1);   printf(" 4. Business   :");
  97.     cup(9,1);   printf(" 5. Street     :");
  98.     cup(10,1);  printf(" 6. City       :");
  99.     cup(11,1);  printf(" 7. State      :");
  100.     cup(12,1);  printf(" 8. Zip        :");
  101.     cup(13,1);  printf(" 9. Home phone :");
  102.     cup(14,1);  printf("10. Work phone :");
  103.     tempkey[0] = 21;
  104.     cup(5,18);
  105.     cgets(tempkey);
  106.     if (tempkey[1] == 0)
  107.       quit = 1;
  108.     else {
  109.       strcpy(addr.name_key,&tempkey[2]);
  110.       edit_it();
  111.     }
  112.   } while(!quit);
  113.   return;
  114. }
  115.  
  116. void edit_it(void)
  117. {
  118.   int item;
  119.   char c,
  120.        trash[6];
  121.  
  122.   if (iread(adr_file,&addr)) {
  123.     cup(23,1); printf("%s Not found.  Add new record? ",addr.name_key);
  124.     if (toupper(getche()) != 'Y')
  125.       return;                   /* not found */
  126.     cup(23,1); el;              /* erase line */
  127.     for (item = 2; item <= 10; change_item(item++));
  128.     if (iwrite(adr_file,&addr)) {
  129.       cup(23,1); printf("Error write.  Status is %X.\n",ierrno);
  130.       get_ret();
  131.       return;
  132.     }
  133.   }
  134.   else {
  135.     cup(5,18);  puts(addr.name_key);
  136.     cup(6,18);  puts(addr.first_name);
  137.     cup(7,18);  puts(addr.last_name);
  138.     cup(8,18);  puts(addr.business);
  139.     cup(9,18);  puts(addr.street);
  140.     cup(10,18); puts(addr.city);
  141.     cup(11,18); puts(addr.state);
  142.     cup(12,18); puts(addr.zip);
  143.     cup(13,18); puts(addr.phone1);
  144.     cup(14,18); puts(addr.phone2);
  145.   }
  146.   do {
  147.     cup(23,1); printf("Enter function * (C,D,Q)");
  148.     cup(23,16);
  149.     switch (c = toupper(getche())) {
  150.       case 'C'  :
  151.         do {
  152.           cup(23,1); el;
  153.           printf("Enter item to change ");
  154.           trash[0] = 3;
  155.           cgets(trash);
  156.           if (trash[1] == 0)
  157.             item = 0;
  158.           else {
  159.             item = atoi(&trash[2]);
  160.             if (item > 1 && item < 11)
  161.               change_item(item);
  162.           }
  163.         } while (item != 0);
  164.         if (irewrite(adr_file,&addr)) { /* update the record */
  165.           cup(23,1);  printf("Error rewrite.  Status is %X\n",ierrno);
  166.           get_ret();
  167.           return;
  168.         }
  169.         break;
  170.       case 'D'  :
  171.         if (idelete(adr_file,&addr)) {
  172.           cup(23,1); printf("Error delete.  Status is %X\n",ierrno);
  173.           get_ret();
  174.           return;
  175.         }
  176.         c = 'Q';
  177.         break;
  178.       case 'Q'  :
  179.         break;
  180.       default   :
  181.         putch('\007');
  182.     } /* switch */
  183.   } while (c != 'Q');
  184. } /* edit_it */
  185.  
  186. void change_item(int item)
  187. {
  188.   char temp[35];
  189.   switch (item) {
  190.     case 2 :
  191.       cup(6,18);
  192.       temp[0] = 31;
  193.       cgets(temp);
  194.       strcpy(addr.first_name,&temp[2]);
  195.       break;
  196.     case 3 :
  197.       cup(7,18);
  198.       temp[0] = 31;
  199.       cgets(temp);
  200.       strcpy(addr.last_name,&temp[2]);
  201.       break;
  202.     case 4 :
  203.       cup(8,18);
  204.       temp[0] = 31;
  205.       cgets(temp);
  206.       strcpy(addr.business,&temp[2]);
  207.       break;
  208.     case 5 :
  209.       cup(9,18);
  210.       temp[0] = 31;
  211.       cgets(temp);
  212.       strcpy(addr.street,&temp[2]);
  213.       break;
  214.     case 6 :
  215.       cup(10,18);
  216.       temp[0] = 26;
  217.       cgets(temp);
  218.       strcpy(addr.city,&temp[2]);
  219.       break;
  220.     case 7 :
  221.       cup(11,18);
  222.       temp[0] = 3;
  223.       cgets(temp);
  224.       strcpy(addr.state,&temp[2]);
  225.       break;
  226.     case 8 :
  227.       cup(12,18);
  228.       temp[0] = 10;
  229.       cgets(temp);
  230.       strcpy(addr.zip,&temp[2]);
  231.       break;
  232.     case 9 :
  233.       cup(13,18);
  234.       temp[0] = 11;
  235.       cgets(temp);
  236.       strcpy(addr.phone1,&temp[2]);
  237.       break;
  238.     case 10 :
  239.       cup(14,18);
  240.       temp[0] = 11;
  241.       cgets(temp);
  242.       strcpy(addr.phone2,&temp[2]);
  243.       break;
  244.   } /* switch */
  245. } /* change_item */
  246.  
  247. void list_forward(void)
  248. {
  249.   ed;
  250.   if (istart(adr_file,START_FILE,&addr)) {
  251.     printf("Error starting file.  Error code is %X\n",ierrno);
  252.     return;
  253.   }
  254.   while (!iread_next(adr_file,&addr))
  255.     printf("%s:  %s %s\n",addr.name_key,addr.first_name,addr.last_name);
  256.   get_ret();
  257. }
  258.  
  259. void list_backward(void)
  260. {
  261.   ed;
  262.   if (istart(adr_file,END_FILE,&addr)) {
  263.     printf("Error starting file.  Error code is %X\n",ierrno);
  264.     return;
  265.   }
  266.   while (!iread_prev(adr_file,&addr))
  267.     printf("%s:  %s %s\n",addr.name_key,addr.first_name,addr.last_name);
  268.   get_ret();
  269. }
  270.  
  271. void cup(int row, int col)
  272. {
  273.   char rows[6],
  274.        cols[6];
  275.   itoa(row,rows,10);
  276.   itoa(col,cols,10);
  277.   printf("\033[%s;%sH",rows,cols);
  278. }
  279.  
  280. void get_ret(void)
  281. {
  282.   cup(24,1);  printf("Press any key ... ");
  283.   getche();
  284. }
  285.  
  286. void usage(void)
  287. {
  288.   printf("Usage: adr <filename>\n");
  289. }
  290.